<?php
/*
@author: wjf
@contact: Hurrican
@software: PhpStorm
@create: 2022/8/9 9:12
*/
declare(strict_types=1);
namespace app\store\controller;

use app\common\library\helper;
use think\App;
use app\store\service\lottery\Manage;
use think\response\Json;


class Lottery extends Controller{

    private $manageService;
    public function __construct(App $app,Manage $manageService)
    {
        parent::__construct($app);
        $this->manageService = $manageService;
    }

    //列表
    public function index():Json
    {
        $params = $this->validate(
            $this->request->get(),
            [
                'page' => 'integer',
                'limit' => 'integer',
                'status' => 'integer',
            ]
        );
        $list = $this->manageService->getIndex($params);
        return $this->renderSuccess(compact('list'));
    }

    //新增
    public function store(): Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'type|类型' => 'require', // 1活动时间开奖  2 满开抽奖
                'participate_type|参与类型' => 'require',
                'points|积分' => 'require|integer',
                'sort|排序' => 'integer',
                'name|名称' => 'require',
                'cover_id|封面' => 'require',   //封面（单图）
               // 'start_time|开始时间' => 'require',
               // 'end_time|结束时间' => 'require',
                'num|中签名额' => 'require',  //中签名额
                'main_image_id|主图' => 'require',  //图片id(主图)
                'status|开启状态' => 'require',
                'task_config|任务配置' => 'require|array',
                'full_open_num|满开人数' => 'require|integer',
                'reward_goods|奖品' => 'require'
            ]
        );
        $type = $params['type'];
        if(!in_array($type,[1,2])) return $this->renderError('type参数错误');
        if($type == 2){
            if($params['points'] <= 0 || $params['full_open_num'] <= 0){
                return $this->renderError('满人开奖模式下积分必须大于0且满开人次必须大于0');
            }
        }
        $res = $this->manageService->add($params);
        if(!$res){
            $this->renderError('新增失败');
        }
        return $this->renderSuccess([],'新增成功');
    }

    //更新
    public function update():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'id' => 'require',
                'type|类型' => 'require', // 1活动时间开奖  2 满开抽奖
                'participate_type|参与类型' => 'require',
                'sort|排序' => 'integer',
                'points|积分' => 'require|integer',
                'name|名称' => 'require',
                'cover_id|封面' => 'require',   //封面（单图）
                // 'start_time|开始时间' => 'require',
                // 'end_time|结束时间' => 'require',
                'num|中签名额' => 'require',  //中签名额
                'main_image_id|主图' => 'require',  //图片id(主图)
                'status|开启状态' => 'require',
                'task_config|任务配置' => 'require|array',
                'full_open_num|满开人数' => 'require|integer',
                'reward_goods' => 'require'
            ]
        );
        $type = $params['type'];
        if(!in_array($type,[1,2])){
            return $this->renderError('type参数错误');
        }
        if($type == 2){
            if($params['points'] <= 0 || $params['full_open_num'] <= 0){
                return $this->renderError('满人开奖模式下积分必须大于0且满开人次必须大于0');
            }
        }
        $res = $this->manageService->update($params);
        if(!$res){
            $this->renderError('更新失败');
        }
        return $this->renderSuccess();
    }

    //删除
    public function delete():Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'id' => 'integer|require',
            ]
        );
        $res = $this->manageService->delete($params);
        if(!$res){
            $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    //开启和关闭
    public function state():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'id' => 'integer|require',
                'status' => 'integer|require'   //1开启  -1关闭
            ]
        );
        if(!in_array($params['status'],[-1,1])){
            return $this->renderError('status参数错误');
        }
        $res = $this->manageService->state($params);
        if(!$res){
            return $this->renderError('系统异常');
        }
        return $this->renderSuccess();
    }

    //详情
    public function detail():Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'id' => 'integer|require',
            ]
        );
        $id = $params['id'];
        $res = $this->manageService->detail($id);
        if(!$res){
            $this->renderError('删除失败');
        }
        return $this->renderSuccess($res);
    }

    //参与详情
    public function participantInfo(): Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'id' => 'integer|require',
                'page' => 'integer',
                'limit' => 'integer'
            ]
        );
        $list = $this->manageService->getParticipantInfo($params);
        return $this->renderSuccess(compact('list'));
    }

}